package com.searchanything;

import java.util.HashMap;

import com.commonfunction.CommFunction;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class SearchResultAction extends ActionSupport{
private String serchBy,searchText,startDate,endDate;
	
	
	public String getStartDate() {
		return startDate;
	}


	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}


	public String getEndDate() {
		return endDate;
	}


	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}


	public String getSerchBy() {
		return serchBy;
	}


	public void setSerchBy(String serchBy) {
		this.serchBy = serchBy;
	}


	public String getSearchText() {
		return searchText;
	}


	public void setSearchText(String searchText) {
		this.searchText = searchText;
	}

	HashMap dataMapComplete = new HashMap();
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		
		String userrole = (String) ActionContext.getContext().getSession().get("userrole"); 
		if ((null != userrole) && ( userrole.equals("admin") || userrole.equals("superadmin"))) {
			
		}else
			return "unauthorised";
		
		SearchAnythingFunctions anythingFunctions = new SearchAnythingFunctions();
		CommFunction commFunction = new CommFunction();
		System.out.println("Calling Search action");
		System.out.println("Serch result Action "+searchText);
		dataMapComplete = new HashMap();
		
		if(searchText == null){
			searchText = (String)ActionContext.getContext().getSession().get("searchText");
		}else{
			ActionContext.getContext().getSession().put("searchText", searchText);
		}
		
		if( null == serchBy){
			serchBy = (String)ActionContext.getContext().getSession().get("serchBy");
		}else{
			ActionContext.getContext().getSession().put("serchBy", serchBy);
		}
		
		if(startDate == null){
			startDate = (String)ActionContext.getContext().getSession().get("startDate");
		}else{
			ActionContext.getContext().getSession().put("startDate", startDate);
		}
		
		if(endDate == null){
			endDate = (String)ActionContext.getContext().getSession().get("endDate");
		}else{
			ActionContext.getContext().getSession().put("endDate", endDate);
		}
		
		
		int startDateInt = commFunction.formatDateForDb(startDate);
		int endDateInt = commFunction.formatDateForDb(endDate);
		dataMapComplete = anythingFunctions.getDealerData(searchText, serchBy,startDateInt , endDateInt);
		ActionContext.getContext().getSession().put("completeData", dataMapComplete);
		return "success";
	}
	
}
